package march.test_3_12;

import java.util.Arrays;

public class Solution {
    public int Add(int num1,int num2) {
        String str1 = Integer.toString(num1,2);
        String str2 = Integer.toString(num2,2);
        str1 = c(str1);
        str2 = c(str2);
        System.out.println(str1);
        System.out.println(str2);
        int min = Math.min(str2.length(),str1.length());
        int max = Math.max(str2.length(),str1.length());
        char[] arr = new char[max + 1];
        for(int i = 0; i < min; i++) {
            if(str1.charAt(i) == '1' && str2.charAt(i) == str1.charAt(i) ) {
                arr[i+1] = '1';
            }
            if(str2.charAt(i) != str1.charAt(i)) {
                if(arr[i] == '1') {
                    arr[i + 1] = '1';
                    arr[i] = '0';
                } else {
                    arr[i] = '1';
                }
            } else {
                arr[i] = '0';
            }
        }
        if(max == str1.length()) {
            for (int i = min; i < max; i++) {
                if(arr[i] == '0' && str1.charAt(i) == '0') {
                    arr[i] = '0';
                } else if(arr[i] != str1.charAt(i)) {
                    arr[i] = '1';
                } else {
                    arr[i] = '0';
                    arr[i + 1] = '1';
                }
            }
        } else {
            for (int i = min; i < max; i++) {
                if(arr[i] == '0' && str2.charAt(i) == '0') {
                    arr[i] = '0';
                } else if(arr[i] != str2.charAt(i)) {
                    arr[i] = '1';
                } else {
                    arr[i] = '0';
                    arr[i + 1] = '1';
                }
            }
        }
        String string = String.valueOf(arr);
        System.out.println(string);
        return 3;
    }
    public String c(String str) {
        int left = 0;
        int right = str.length() - 1;
        while (left < right) {
            char tmp = str.charAt(left);
            str.replace(str.charAt(left),str.charAt(right));
            str.replace(str.charAt(right),tmp);
            left++;
            right--;
        }
        System.out.println(str);
        return str;
    }
}